// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import qs from 'qs'
import store from './vuex/store'


import 'vue-ydui/dist/ydui.flexible.js';
import 'vue-ydui/dist/ydui.base.css';
import './assets/css/yzp.less'

import '../static/ueditor/ueditor.config'
import '../static/ueditor/ueditor.all'
import '../static/ueditor/lang/zh-cn/zh-cn'


import {Toast,Loading} from 'vue-ydui/dist/lib.rem/dialog';

import {getCookie} from './assets/js/utils'

const FastClick = require('fastclick');


Vue.prototype.$axios = axios;
axios.defaults.timeout = 20000;
axios.defaults.baseURL = 'http://127.0.0.1:5000';   //配置接口地址
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.withCredentials = true;
//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
  Loading.open("loading...");
  //在发送请求之前做某件事
  if (config.method === 'post') {

    config.data = qs.stringify(config.data);
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});


//添加响应拦截器
axios.interceptors.response.use((response) => {
  //对响应数据做些事
  Loading.close();
  return response;
}, (error) => {
  //请求错误时做些事
  let errMsg = '';
  let str = error.toString();
  if (str.indexOf('timeout') > 0) {
    errMsg = "请求超时，请刷新后重试！"
  }
  else {
    errMsg = "网络异常！"
  }
  Loading.close();
  Toast({mes: errMsg});
  return Promise.reject(error);
});



router.beforeEach((route, redirect, next) => {
console.log(getCookie("user"))
  if (route.matched.some(m => m.meta.auth)) {
    // 对路由进行验证
    if (getCookie("user")) {
      next()
    } else {

      next({path: '/login', query: {referrer: route.fullPath}});
    }
  } else {

    next()
  }


  Loading.close();
});

router.afterEach(route => {

  Loading.close();
});


document.addEventListener('DOMContentLoaded', function () {
  FastClick.attach(document.body);
}, false);

Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: {App}
});
